//https://www.yuque.com/wangpengfei-kgu2c/dupr81/azlfno#EI27D


M=require("ming_node");

app=M.server()
app.listen(8090)

app.set("views",".")

global.CONFIG={
    "appid": "wxb82ed6f75c3a22ad",
    "appsecret": "999915ad9371781036b2bf8f917a28fd",
}
appid="wxb82ed6f75c3a22ad"
appsecret="999915ad9371781036b2bf8f917a28fd"


app.get("/tokenVerify",(req,res)=>{
    res.send(req.params.echostr)
})


app.use("/member/index",async (req,res)=>{
    //说明小程序,已经登陆过
    if(req.headers["token"]){
        app.redirect("/index",req,res)
        return;
    }
    //小程序没有登陆过,将小程序重定向到授权页
    let authLoginUrl=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${global.CONFIG.appid}&redirect_uri=https://actiondev.langjie.com/wx/wxcallback&response_type=code&scope=snsapi_userinfo&state=http://localhost:8090/wx/wxcallback/local&connect_redirect=1#wechat_redirect`
    app.redirect(authLoginUrl,req,res)
})


//这个接口要公网可访问,授权完,微信服务器重定向到这里,这里根据state并带上code再次重定向到本地 /wx/wxcallback/local
app.get("/wx/wxcallback",async (req,res)=>{
    console.log("远程线上机=======>",req.url)
    let redirectUrl=`${req.params.state}?code=${req.params.code}`
    console.log(redirectUrl)
    //重定向到 http://localhost:8888/wx/wxcallback/local
    app.redirect(redirectUrl,req,res)
})

/**
 这个是自己的公网服务器让小程序重定向到了这里,小程序会带上code, cookie如果不管用了,改成token,代码维护这个token
 =====>accessTokenResult { access_token: '44_-Rirb580etsSwoIXXpl5MWgnDifCateeEm7Gfs0d5OVWcxKJ7uPsypDgQxDwOjnQhnDuYhrgr7ugpJmvpx9ZPQAa8mnyCGnhHg9qI5PY6nw',
  expires_in: 7200,
  refresh_token: '44_CLKLq8Pw9Na5PIS2NbHLInF8kn5jpaO4xfp2sfHQbcMWsLxHuJF_FKZOz1oiURrnHOO3otY8FZOrcMSqGfiimwUkJ4qdLamYB2hGq4YYQOw',
  openid: 'ooO7L6jctygDM49HXqf8_8CX71kM',
  scope: 'snsapi_userinfo',
  unionid: 'oathZ1BuSm9Iblq83ND8qxlfHfmI' }

 =====>userInfoResult { openid: 'ooO7L6jctygDM49HXqf8_8CX71kM',
  nickname: '王朋飞',
  sex: 1,
  language: 'zh_CN',
  city: '洛阳',
  province: '河南',
  country: '中国',
  headimgurl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLoIEH4ocPRE6bJxsbkIMFHLcGsNzhR3Y9oA3BE950KZA0wbjfeZIQfD48V6g9PK5h3J1MZSQNdFg/132',
  privilege: [],
  unionid: 'oathZ1BuSm9Iblq83ND8qxlfHfmI' }
 */
app.get("/wx/wxcallback/local",async (req,res)=>{
    console.log("本机=======>",req.url)
    //code 换 accessToken,code 只能用一次
    getTokenUrl=`https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${appsecret}&code=${req.params.code}&grant_type=authorization_code`
    accessTokenResult=await M.get(getTokenUrl)
    console.log("=====>accessTokenResult",accessTokenResult)
    //accessToken 换 userInfo
    userInfoUrl=`https://api.weixin.qq.com/sns/userinfo?access_token=${accessTokenResult.access_token}&openid=${accessTokenResult.openid}&lang=zh_CN`
    userInfoResult=await M.get(userInfoUrl)
    console.log("=====>userInfoResult",userInfoResult)
    //设置一下session 说明用户已经登陆
    //req.session={openid:accessTokenResult.openid,userInfoResult}
    app.redirect("/index.html?openid="+accessTokenResult.openid,req,res)
})



app.get("proxyScan",async (req,res)=>{
    var params = req.params;
    var page = params.page||"http://localhost:8090/";
    console.log(params,"AAAAAAAAA")
    var timestamp = params.timestamp|| Date.now();
    const APPID = CONFIG.appid;
    const APPSECRET = CONFIG.appsecret;
    var getToken="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
    r1= await  M.get(getToken)
    var access_token = r1.access_token
    var getticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+access_token+"&type=jsapi";
    r2= await  M.get(getticket)
    var ticket = r2.ticket;
    var string = 'jsapi_ticket=' + ticket + '&noncestr=Wm3WZYTPz0wzccnW&timestamp=' + timestamp + '&url=' + page;
    var sha1 = require('sha1');
    var signature = sha1(string);
    res.send(M.successResult({
        signature: signature,
        appId: APPID,
        nonceStr: "Wm3WZYTPz0wzccnW"
    }))
});
